<template>
  <div class="page">
    <div class=" search margin-bottom-sm">
      <mt-cell title="箱号：">
        <mt-field
          placeholder="输入完敲回车查询箱号"
          v-model="boxNo"
          ref="ref_1"
          @keyup.enter.native="queryAndprintWaybillLabel"
        ></mt-field>
        <mt-button
          size="small"
          type="primary"
          @click="queryAndprintWaybillLabel"
          >查询</mt-button
        >
      </mt-cell>
      <mt-cell title="库区：" class="flex">
        <mt-field
          placeholder="请输入库区"
          v-model="location"
          ref="ref_2"
          style="width:76%"
        ></mt-field>
        <mt-button
          size="small"
          type="primary"
          @click="saveAssociateWarehouse"
          style="width:24%"
          >保存</mt-button
        >
      </mt-cell>
    </div>
    <div>
      <mt-cell title="运单号：" :value="waybillNo"></mt-cell>
      <mt-cell title="客户：" :value="customerName"></mt-cell>
      <mt-cell title="国家：" :value="country"></mt-cell>
      <mt-cell title="入库渠道：" :value="inStockName"></mt-cell>
      <mt-cell title="客户件数：" :value="customerCount"></mt-cell>
      <mt-cell
        title="到仓件数："
        :value="arrivalPackages ? arrivalPackages : 0"
      ></mt-cell>
      <mt-cell title="订单状态：" :value="statusName"></mt-cell>
      <mt-cell title="销售：" :value="saleName"></mt-cell>
      <mt-cell title="库区：" :value="reservoirArea"></mt-cell>
      <mt-cell title="耗时（毫秒）：" :value="time"></mt-cell>
    </div>
  </div>
</template>
<script>
import { ajax } from "@/store/yg/common/utils";
import axios from "axios";
import * as constant from "@/store/yg/common/constant";
import { Toast } from "mint-ui";

export default {
  components: {},
  props: [],
  data() {
    return {
      boxNo: "",
      reservoirArea: "",
      waybillNo: "",
      customerName: "",
      inStockName: "",
      customerCount: "",
      arrivalPackages: "",
      customerWay: "",
      statusName: "",
      status: "",
      saleName: "",
      time: "",
      waybillId: "",
      reservoirAreaShow: "",
      country: "",
      warehouseRegionSource: {},
      location: ""
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.initDat();
  },
  methods: {
    initDat() {
      this.$refs.ref_1.$refs.input.focus();
    },
    queryAndprintWaybillLabel() {
      if (!this.boxNo) {
        Toast("请输入箱号");
        return;
      }
      if (this.boxNo.length < 6) {
        this.$message.error("请输入正确的箱号");
        return;
      }
      ajax(
        "doAssociateWarehouse",
        { boxNo: this.boxNo },
        { type: "mobile" }
      ).then(
        res => {
          console.log(res);
          if (res.code == 200) {
            var data = res.data;
            this.waybillNo = data.waybillNo;
            this.customerName = data.customerName;
            this.inStockName = data.inStockName;
            this.customerCount = data.customerCount;
            if (data.arrivalPackages == null || data.arrivalPackages == "") {
              this.arrivalPackages = 0;
            } else {
              this.arrivalPackages = data.arrivalPackages;
            }
            this.customerWay = data.customerWay;
            this.statusName = data.statusName;
            this.status = data.status;
            this.saleName = data.saleName;
            this.time = data.time;
            this.waybillId = data.waybillId;
            this.reservoirAreaShow = data.reservoirAreaShow;
            this.warehouseRegionSource = data.warehouseRegionSource;
            this.country = data.country;
            this.$refs.ref_2.$refs.input.focus();
          } else {
            Toast("操作失败"+res.message);
          }
        },
        error => {
          console.log(error);
          this.$router.push("/login");
        }
      );
    },
    saveAssociateWarehouse() {
      if (!this.waybillId) {
        Toast("运单不存在");
        return false;
      }
      if (!this.location) {
        Toast("请输入库区");
        return false;
      }
      if (
        !(
          (this.status && parseInt(this.status) == -10) ||
          parseInt(this.status) <= 50
        )
      ) {
        Toast("运单状态需为待入库，出库，冻结状态");
        return false;
      }
      if (
        !this.arrivalPackages ||
        (this.arrivalPackages && parseInt(this.arrivalPackages) < 0)
      ) {
        //Toast("当前订单到仓件数为空");
      }
      ajax(
        "saveAssociateWarehouse",
        {
          waybillId: this.waybillId,
          reservoirArea: this.location
        },
        {
          type: "mobile"
        }
      ).then(
        res => {
          console.log(res);

          if (res.code == 200) {
            // this.$message.success("保存成功");
            this.boxNo = "";
            this.location = "";
            this.waybillNo = "";
            this.customerName = "";
            this.inStockName = "";
            this.customerCount = "";
            this.arrivalPackages = "";
            this.customerWay = "";
            this.statusName = "";
            this.status = "";
            this.saleName = "";
            this.time = "";

            Toast({
              message: "保存成功",
              iconClass: "icon icon-success"
            });
            this.$refs.ref_1.$refs.input.focus();
          } else {
            Toast("操作失败"+res.message);
          }
        },
        error => {
          console.log(error);
        }
      );
    }
  }
};
</script>
<style></style>
<style scoped>
.page {
  background: #f5f5f6;
}
.page >>> .search .mint-cell-value {
  flex: 3;
}
</style>
